package agenda.server;




import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import agenda.client.ContatosService;
import agenda.shared.model.Contato;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class ContatosServiceImpl extends RemoteServiceServlet implements
		ContatosService {
	private static final String PERSISTENCE_UNIT_NAME = "DB";
	private static EntityManagerFactory factory;
	
	
	public String ContatosServer(Contato contato) throws IllegalArgumentException {
		
		factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		EntityManager em = factory.createEntityManager();
		
		// Create new todo
		em.getTransaction().begin();		
		em.persist(contato);		
		em.getTransaction().commit();
		em.close();
	
		return contato.toString();
		
	}
	
	
	public Contato getContato(long Codigo) throws IllegalArgumentException {
		
		
		factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		EntityManager em = factory.createEntityManager();
		
		// Create new todo
		
		return em.find(Contato.class, Codigo);			
			
	}


	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	/*private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}
	*/
}
